package com.homepage.web.rest;

import com.homepage.repository.FeaturetteImageRepository;
import com.homepage.service.FeaturetteImageService;
import com.homepage.service.dto.FeaturetteImageDTO;
import com.homepage.util.HeaderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * REST controller for managing {@link com.homepage.domain.FeaturetteImage}.
 */
@RestController
@RequestMapping("/company")
public class FeaturetteImageResource {

    private final Logger log = LoggerFactory.getLogger(FeaturetteImageResource.class);

    private static final String ENTITY_NAME = "featuretteImage";

    @Value("${common.clientApp.name}")
    private String applicationName;

    private final FeaturetteImageService featuretteImageService;

    private final FeaturetteImageRepository featuretteImageRepository;

    public FeaturetteImageResource(FeaturetteImageService featuretteImageService, FeaturetteImageRepository featuretteImageRepository) {
        this.featuretteImageService = featuretteImageService;
        this.featuretteImageRepository = featuretteImageRepository;
    }


    @PutMapping(value = "/featurette/{id}/image", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    @ResponseBody
    public ResponseEntity<ModelMap>  createFeaturetteImage(@PathVariable Long id,
                                                            @RequestPart MultipartFile picData) throws URISyntaxException, IOException {
        log.debug("REST request to save featurette image" );
        InputStream is = picData.getInputStream();
        String contentType = picData.getContentType();
        String fileName = picData.getOriginalFilename();
        byte[] pictureData = new byte[(int) picData.getSize()];
        int length = is.read(pictureData);
        FeaturetteImageDTO featuretteImageDTO = new FeaturetteImageDTO();
        featuretteImageDTO.setFeaturetteId(id);
        featuretteImageDTO.setFeaturetteImage(pictureData);
        featuretteImageDTO.setFeaturetteImageContentType(contentType);

        featuretteImageService.save(featuretteImageDTO);

        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("code", HttpStatus.CREATED.value());
        return ResponseEntity
                .created(new URI("/featurette/"+id+"/image" ))
                .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME,  String.valueOf(id)))
                .body(modelMap);
    }


    /**
     * {@code GET  /featurette-images} : get all the featuretteImages.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of featuretteImages in body.
     */
    @GetMapping("/featurette/{id}/image")
    public ResponseEntity<byte[]> getFeaturetteImageByFeaturatteId(@PathVariable(value = "id")  long id){
        FeaturetteImageDTO featuretteImageDTO = featuretteImageService.findOneByFeaturetteId(id).get();
        if(featuretteImageDTO == null)
        {
            return new ResponseEntity(null,null, HttpStatus.NOT_FOUND);
        }
        HttpHeaders headers = new HttpHeaders();
        if(featuretteImageDTO.getFeaturetteImageContentType().equalsIgnoreCase(MediaType.IMAGE_JPEG_VALUE))
            headers.setContentType(MediaType.IMAGE_JPEG);
        if(featuretteImageDTO.getFeaturetteImageContentType().equalsIgnoreCase(MediaType.IMAGE_PNG_VALUE))
            headers.setContentType(MediaType.IMAGE_PNG);
        headers.setContentLength(featuretteImageDTO.getFeaturetteImage().length);

        return new ResponseEntity<byte[]>(featuretteImageDTO.getFeaturetteImage(), headers, HttpStatus.OK);
    }

    /**
     * {@code DELETE  /featurette-images/:id} : delete the "id" featuretteImage.
     *
     * @param id the id of the featuretteImageDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/featurette-images/{id}")
    public ResponseEntity<Void> deleteFeaturetteImage(@PathVariable Long id) {
        log.debug("REST request to delete FeaturetteImage : {}", id);
        featuretteImageService.delete(id);
        return ResponseEntity
            .noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
            .build();
    }
}
